package com.adcc.swingui.factory;

import java.awt.BorderLayout;
import java.awt.Container;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.adcc.swingui.base.DefaultStatusBar;
import com.adcc.swingui.bean.StatusBean;
import com.adcc.swingui.bean.StatusComponentBean;
import com.adcc.swingui.conf.StatusBarConf;
import com.jidesoft.docking.DefaultDockableHolder;
import com.jidesoft.status.StatusBar;
import com.jidesoft.swing.JideBoxLayout;

/**
 * 状态栏
 * 
 * @author charlie
 * 
 */
public class StatusBarFactory {
	private static Logger logger = Logger.getLogger(StatusBarFactory.class);
	private static Map<String, StatusBar> map = new HashMap<String, StatusBar>();

	public static void create(String[] name, Container container) {
		for (int i = 0; i < name.length; i++) {
			if (map.containsKey(name[i])) {
				logger.warn("状态栏对象已经存在" + name[i]);
			}
			StatusBean bean = StatusBarConf.getStatusConf().get(name[i]);
			StatusBar statusBar = new StatusBar();
			if (bean != null) {
				for (StatusComponentBean component : bean.getStatusList()) {
					try{
						Class c = Class.forName(component.getClassName());
						Constructor con = c.getConstructor(new Class[] { Container.class });
						DefaultStatusBar o = (DefaultStatusBar) con.newInstance(container);
						statusBar.add(o.buildButton(o.getClass().getName()),
				        		JideBoxLayout.FLEXIBLE);
					}catch(Exception e){
						logger.error("工具创建失败" + component.getClassName() + e.getMessage());
					}
					
				}
			} else {
				logger.error("状态栏配置不存在" + name[i]);
			}
			// 菜单缓存到内存
			map.put(name[i], statusBar);
			// 设置到主容器中
			((DefaultDockableHolder) container).getContentPane().add(statusBar, BorderLayout.AFTER_LAST_LINE);
		}
	}
}
